/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.BookInfo;

import com.nc.kyiv.yellow.sociallibrary.similarUse.BookInfoInterface;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookAuthor;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author Alena
 */
public class BookAuthors implements BookInfoInterface{

    @Override
    public List<BookAuthor> getBookInfo(String isbn, Connection conn) throws SQLException, Exception {
        
        ResultSet rs = null; 
        CallableStatement cStmt = null;
        ArrayList<BookAuthor> bookAuthorsArray = new ArrayList<>();
       
        try {
           
            cStmt = conn.prepareCall("{call GET_BOOK_AUTHORS(?,?)}");
            cStmt.setString(1, isbn);
            cStmt.registerOutParameter(2, OracleTypes.CURSOR);          
            cStmt.execute();
            rs = (ResultSet) cStmt.getObject(2);

            if (rs.next()){
                while (!rs.isAfterLast()) {
                    bookAuthorsArray.add(new BookAuthor(rs.getString("first_name"), rs.getString("middle_name"), rs.getString("last_name")));
                    rs.next();
                }
            }
        } catch (SQLException ex) {
            throw ex;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            }
            finally {
                    if (cStmt != null) {
                        cStmt.close();                    
                    }
            }
        }
      return bookAuthorsArray;       
    }   
}
